package com.eduhzy.ttw.clazz.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.clazz.R;
import com.eduhzy.ttw.clazz.R2;
import com.eduhzy.ttw.clazz.di.component.DaggerAddGroupComponent;
import com.eduhzy.ttw.clazz.di.module.AddGroupModule;
import com.eduhzy.ttw.clazz.mvp.contract.AddGroupContract;
import com.eduhzy.ttw.clazz.mvp.model.entity.HomeData;
import com.eduhzy.ttw.clazz.mvp.model.entity.ReviewStudentData;
import com.eduhzy.ttw.clazz.mvp.presenter.AddGroupPresenter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.umeng.analytics.MobclickAgent;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-12-7 9:15:38
 */
@Route(path = RouterHub.CLAZZ_ADDGROUPACTIVITY)
public class AddGroupActivity extends BaseActivity<AddGroupPresenter> implements AddGroupContract.View {
    @Autowired(name = Constants.PARCELABLE_DATA)
    HomeData mBean;
    @Autowired(name = Constants.ROUTER_INTEGER)
    int teamType;
    @BindView(R2.id.emptyView)
    QMUIEmptyView mEmptyView;
    @BindView(R2.id.et_title)
    EditText mEtTitle;
    @BindView(R2.id.rv_list)
    RecyclerView mRvList;
    @BindView(R2.id.btn_submit)
    QMUIRoundButton mBtnSubmit;
    @Inject
    BaseQuickAdapter<ReviewStudentData, AutoBaseViewHolder> mAdapter;
    @Inject
    RecyclerView.LayoutManager mLayoutManager;
    private StringBuilder studentIds;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerAddGroupComponent
                .builder()
                .appComponent(appComponent)
                .addGroupModule(new AddGroupModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.clazz_activity_add_group;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mRvList.setLayoutManager(mLayoutManager);
        mRvList.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            ReviewStudentData studentData = mAdapter.getData().get(position);
            studentData.setCheck(!studentData.isCheck());
            mAdapter.notifyItemChanged(position);
        });
        getData();
    }

    private void getData() {
        if (ObjectUtils.isNotEmpty(mBean)) {
            mPresenter.getUngroupUser(mBean.getClassId(), teamType);
        }
    }

    @Override
    public void showLoading() {
        mEmptyView.show(true);
    }

    @Override
    public void hideLoading() {
        mEmptyView.hide();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @OnClick(R2.id.btn_submit)
    public void onViewClicked() {
        if (RxUtil.showEditTip(this, mEtTitle, mEtTitle.getHint().toString())) {
            return;
        }
        studentIds = new StringBuilder();
        for (ReviewStudentData studentData : mAdapter.getData()) {
            if (studentData.isCheck())
                studentIds.append(studentData.getStudentId() + ",");
        }

        String teamName = mEtTitle.getText().toString();
        if (ObjectUtils.isEmpty(studentIds.toString())) {
            showMessage("请选择小组成员");
            return;
        }
        MobclickAgent.onEvent(getActivity(),"ttw_group_add");
        mPresenter.saveTeam(mBean.getClassId(), teamType, teamName, studentIds.toString());

    }
}
